package com.roomaja.roomaja.controller;

import com.alibaba.fastjson.JSONArray;
import com.roomaja.roomaja.entity.BackgroundPictureEntity;
import com.roomaja.roomaja.entity.WenKuEntity;
import com.roomaja.roomaja.mapper.BackgroundPictureMapper;
import com.roomaja.roomaja.mapper.WenKuEntityMapper;
import com.roomaja.roomaja.service.WenKuPageProcessor;
import com.roomaja.roomaja.tool.PageBean;
import com.roomaja.roomaja.tool.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import us.codecraft.webmagic.Spider;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * controller层
 */

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    BackgroundPictureMapper backgroundPictureMapper;
    @Autowired
    WenKuEntityMapper wenKuEntityMapper;
    @Autowired
    WenKuPageProcessor wenKuPageProcessor;
    @Autowired
    HttpServletRequest request;

    private Logger logger = Logger.getLogger(TestController.class);

    /**
     * 访问首页
     * @return
     */
    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public ModelAndView index(){
        ModelAndView model = new ModelAndView("index");
        return model;
    }

    /**
     * 获取背景图片
     * @return
     */
    @RequestMapping(value = "/getPicture",method = RequestMethod.GET)
    public List<BackgroundPictureEntity> getPicture(){
        logger.info("getText --------------------------");
        List<BackgroundPictureEntity> list = backgroundPictureMapper.getAll();
        for (BackgroundPictureEntity entity: list) {
            logger.info(entity.toString());
        }
        return list;
    }

    /**
     * 获取文档的搜索结果
     * @param docTitle
     * @return
     */
    @RequestMapping(value = "/getDocument",method = RequestMethod.GET)
    public ModelAndView getDocument(@RequestParam("doc_title") String doc_title, @RequestParam("currentPage") String currentPage){
        if("".equals(currentPage)){
            currentPage = "1";
        }
        System.out.println(doc_title + "--" + currentPage);
        logger.info("getText --------------------------");
        PageBean<WenKuEntity> pageBean = wenKuPageProcessor.getDocumentsByPage(Integer.parseInt(currentPage), doc_title.trim());
        ModelAndView model = new ModelAndView("resultList");
        // 查询得到的文档
        model.getModel().put("resultList", pageBean.getDocuments());
        // 总页数
        model.getModel().put("totalPage", pageBean.getTotalPage());
        System.out.println("总页数" + pageBean.getTotalPage());
        model.getModel().put("doc_title", doc_title);
        // 总记录数
        model.getModel().put("totalNum", pageBean.getTotalNum());
        model.getModel().put("currentPage", currentPage);
        return model;
    }

    /**
     * 根据页码获取文档的搜索结果
     * @param doc_title
     * @param currentPage
     * @return
     */
    @RequestMapping(value = "/getDocumentByPages",method = RequestMethod.GET)
    public String getDocumentByPages(@RequestParam("doc_title") String doc_title, @RequestParam("currentPage") String currentPage){
        System.out.println(currentPage);
        if("".equals(currentPage)){
            currentPage = "1";
        }
        System.out.println(doc_title + "--" + currentPage);
        logger.info("getText --------------------------");
        PageBean<WenKuEntity> pageBean = wenKuPageProcessor.getDocumentsByPage(Integer.parseInt(currentPage), doc_title);
        JSONArray jsonArray = StringUtils.wenkuEntityList2Json(pageBean.getDocuments());
        return jsonArray.toString();
    }

    /**
     * 跳转到文档详细信息页面
     * @param doc_id
     * @return
     */
    @RequestMapping("/getDetailByDocId")
    public ModelAndView getDetailByDocId(@RequestParam("doc_id") String doc_id){
        WenKuEntity wenKuEntity = wenKuPageProcessor.getDetailByDocId(doc_id);

        ModelAndView modelAndView = new ModelAndView("details");
        if(wenKuEntity == null){
            logger.error("文档不存在");
        }
        modelAndView.getModel().put("wenku", wenKuEntity);
        return modelAndView;
    }

    /**
     * 下载量增加
     * @param doc_id
     * @return
     */
    @RequestMapping("/updateDownloads")
    public String updateDownloads(@RequestParam("doc_id") String doc_id){

        return "success";
    }

    /**
     * 文档信息入库
     */
    @RequestMapping(value = "/add",method = RequestMethod.GET)
        public void add(){
            logger.info("addwenku --------------------------");
       /* WenKuEntity wenKuEntity = new WenKuEntity();
        wenKuEntity.setDocId("1111");
        wenKuEntity.setDocType(WenKuDocTypeEnum.DOC);
        wenKuEntity.setDocTitle("3333");
        wenKuEntityMapper.insert(wenKuEntity);*/
        Spider.create(wenKuPageProcessor).thread(10).addUrl("https://wenku.baidu.com/?l=7.1").start();
    }
}
